package com.learn.dubbo.server;

import com.learn.dubbo.services.UserService;
import com.learn.dubbo.services.impl.UserServiceImpl;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;

import java.io.IOException;
import java.lang.management.ManagementFactory;

/**
 * @author wangxing
 * @version 2020/6/8 11:23 Administrator
 */
public class DubboServer {
    public static void main(String[] args) throws IOException {
        for (int i = 0; i < 2; i++) {
            new Thread(() -> {
                DubboServer d = new DubboServer();
                try {
                    d.openServer(-1);//-1为让dubbo自动去默认一个端口默认为20880
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.println("启动服务端:");
                try {
                    System.in.read();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }

    public void openServer(int port) throws IOException {
//        应用
        ApplicationConfig applicationConfig = new ApplicationConfig("learnDubboServer");
//        协议
        ProtocolConfig protocolConfig = new ProtocolConfig("dubbo", port);
        protocolConfig.setThreads(20);
//        注册中心
//          不使用注册中心
//        RegistryConfig registryConfig = new RegistryConfig(RegistryConfig.NO_AVAILABLE);
//        使用虚拟注册中心
        RegistryConfig registryConfig = new RegistryConfig("multicast://224.1.1.1:3333");
//        服务
        ServiceConfig<UserService> serviceServiceConfig = new ServiceConfig<>();

        serviceServiceConfig.setInterface(UserService.class);
        serviceServiceConfig.setRef(new UserServiceImpl());
        serviceServiceConfig.setRegistry(registryConfig);
        serviceServiceConfig.setProtocol(protocolConfig);
        serviceServiceConfig.setApplication(applicationConfig);
        serviceServiceConfig.export();
        System.out.println("DubboServer服务暴露");
    }
}

